﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using PanGu;

namespace Wei8808Cms.Utility
{
    public class PanGuWrod
    {
        private static readonly PanGu.Match.MatchOptions Options = new PanGu.Match.MatchOptions();
        private static readonly PanGu.Match.MatchParameter Parameters = new PanGu.Match.MatchParameter();
        /// <summary>
        /// 盘古分词
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string StrSplit(string str)
        {
            try
            {
                var watch = new Stopwatch();
                watch.Start();
                var segment = new Segment();

                var words = segment.DoSegment(str, Options, Parameters);

                watch.Stop();

                var wordsString = new StringBuilder();
                foreach (var wordInfo in words.Where(wordInfo => wordInfo != null && wordInfo.Word.Length > 1))
                {
                    wordsString.AppendFormat("{0},", wordInfo.Word);
                }

                return wordsString.ToString().TrimEnd(',');
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}
